/**
 *                            License
 * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS  
 * CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). 
 * THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW.  
 * ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR  
 * COPYRIGHT LAW IS PROHIBITED.
 * 
 * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND  
 * AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE  
 * MAY BE CONSIDERED TO BE A CONTRACT,
 * THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED 
 * HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
 * 
 */
package com.lineage.server.datatables;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.xml.sax.SAXException;

import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.item.action.L1ItemAction;
import com.lineage.server.model.item.action.L1ItemXmlParser;
import com.lineage.server.utils.FileUtil;
import com.lineage.server.utils.PerformanceTimer;
import com.lineage.server.utils.collections.Lists;

/**
 * Class <code>ItemActionTable</code> 道具动作资料表.
 * 
 * @author jrwz
 * @version 2012-6-20下午07:54:40
 * @see com.lineage.server.datatables
 * @since JDK1.7
 */
public final class ItemActionTable {
    /** 提示信息. */
    private static final Logger LOG = Logger.getLogger(ItemActionTable.class
            .getName());
    /** 实例. */
    private static ItemActionTable instance;

    /**
     * 取得实例(当一次实例为空时才新建一个).
     * 
     * @return 实例
     */
    public static ItemActionTable getInstance() {
        return instance;
    }

    /**
     * 加载.
     */
    public static void load() {
        try {
            final PerformanceTimer timer = new PerformanceTimer();
            System.out.print("╠》正在读取 ItemAction...");
            instance = new ItemActionTable();
            System.out.println("完成!\t\t耗时: " + timer.elapsedTimeMillis()
                    + "\t毫秒");
        } catch (final Exception e) {
            LOG.log(Level.SEVERE, "无法加载ItemAction", e);
            System.exit(0);
        }
    }

    /** 道具动作集合. */
    private final List<L1ItemAction> actions = Lists.newList();

    /**
     * 道具动作资料表.
     * 
     * @throws Exception
     *             -
     */
    private ItemActionTable() throws Exception {
        final File usersDir = new File("./data/xml/ItemActions/users/");
        if (usersDir.exists()) {
            this.loadDirectoryActions(usersDir);
        }
        this.loadDirectoryActions(new File("./data/xml/ItemActions/"));
    }

    /**
     * 取得集合内的动作.
     * 
     * @param pc
     *            - 对象
     * @param item
     *            - 道具
     * @return 没有找到则返回null
     */
    public L1ItemAction get(final L1PcInstance pc, final L1ItemInstance item) {
        for (final L1ItemAction action : this.actions) {
            if (action.acceptsRequest(pc, item)) {
                return action;
            }
        }
        return null;
    }

    /**
     * 加载动作.
     * 
     * @param file
     *            - 文件
     * @throws Exception
     *             -
     */
    private void loadAction(final File file) throws Exception {
        this.actions.addAll(this.loadAction(file, "道具动作列表"));
    }

    /**
     * 加载动作.
     * 
     * @param file
     *            - 文件
     * @param nodeName
     *            - 名称
     * @throws ParserConfigurationException
     * 
     *             -
     * @throws SAXException
     *             -
     * @throws IOException
     *             -
     */
    private List<L1ItemAction> loadAction(final File file, final String nodeName)

    throws ParserConfigurationException, SAXException, IOException {
        final DocumentBuilder builder = DocumentBuilderFactory.newInstance()
                .newDocumentBuilder();
        final Document doc = builder.parse(file);

        if (!doc.getDocumentElement().getNodeName().equalsIgnoreCase(nodeName)) {
            return Lists.newList();
        }
        return L1ItemXmlParser.listActions(doc.getDocumentElement());
    }

    /**
     * 加载目录中的动作.
     * 
     * @param dir
     *            文件
     * @throws Exception
     *             -
     */
    private void loadDirectoryActions(final File dir) throws Exception {
        for (final String file : dir.list()) {
            final File f = new File(dir, file);
            if (FileUtil.getExtension(f).equalsIgnoreCase("xml")) {
                this.loadAction(f);
                // this.loadTalkAction(f);
            }
        }
    }
}
